﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Opds_Viewer.ViewModel
{
    public class MainViewModel : ViewModelBase
    {

        #region Private fields

        private UIElement _currentPage;

        // Booleans to know which page is active in the menu
        private bool _isHomeActive;
        private bool _isSettingsActive;
        private bool _isAboutActive;

        // Commanding
        private readonly RelayCommand _showHomeCommand;
        private readonly RelayCommand _showSettingsCommand;
        private readonly RelayCommand _showAboutCommand;

        // List of the commands
        private List<RelayCommand> _commands = new List<RelayCommand>();

        #endregion

        #region Constructor

        public MainViewModel()
        {
            // Initialization of the commands
            _showHomeCommand = new RelayCommand(() =>
                                                    {
                                                        CurrentPage = new View.HomeView();
                                                        IsHomeActive = true;
                                                        IsSettingsActive = false;
                                                        IsAboutActive = false;
                                                    },
                                                () => !(this.CurrentPage is View.HomeView));
            _showSettingsCommand = new RelayCommand(() =>
                                                     {
                                                         CurrentPage = new View.ParameterView();
                                                         IsHomeActive = false;
                                                         IsSettingsActive = true;
                                                         IsAboutActive = false;
                                                     },
                                                 () => !(this.CurrentPage is View.ParameterView));
            _showAboutCommand = new RelayCommand(() =>
                                                    {
                                                        CurrentPage = new View.AboutView();
                                                        IsHomeActive = false;
                                                        IsSettingsActive = false;
                                                        IsAboutActive = true;
                                                    },
                                                () => !(this.CurrentPage is View.AboutView));
            _commands.Add(_showHomeCommand);
            _commands.Add(_showSettingsCommand);
            _commands.Add(_showAboutCommand);

            // Init first displayed view (home)
            if (_showHomeCommand.CanExecute(null)) _showHomeCommand.Execute(null);
        }

        #endregion

        #region public properties

        /// <summary>
        /// Current page (view) displayed in the central content presenter
        /// </summary>
        public UIElement CurrentPage
        {
            get { return _currentPage; }
            set
            {
                _currentPage = value;
                RaisePropertyChanged("CurrentPage");
            }
        }

        /// <summary>
        /// Indicates whether the active page is home or not.
        /// </summary>
        public bool IsHomeActive
        {
            get { return _isHomeActive; }
            set
            {
                _isHomeActive = value;
                RaisePropertyChanged("IsHomeActive");
            }
        }

        /// <summary>
        /// Indicates whether the active page is settings or not.
        /// </summary>
        public bool IsSettingsActive
        {
            get { return _isSettingsActive; }
            set
            {
                _isSettingsActive = value;
                RaisePropertyChanged("IsSettingsActive");
            }
        }

        /// <summary>
        /// Indicates whether the active page is about or not.
        /// </summary>
        public bool IsAboutActive
        {
            get { return _isAboutActive; }
            set
            {
                _isAboutActive = value;
                RaisePropertyChanged("IsAboutActive");
            }
        }

        #endregion

        #region Commanding

        /// <summary>
        /// Show home view
        /// </summary>
        public ICommand ShowHomeView
        {
            get { return _showHomeCommand; }
        }

        /// <summary>
        /// Show parameter view
        /// </summary>
        public ICommand ShowSettingsView
        {
            get { return _showSettingsCommand; }
        }

        /// <summary>
        /// Show about view
        /// </summary>
        public ICommand ShowAboutView
        {
            get { return _showAboutCommand; }
        }

        #endregion
    }
}